﻿using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using Nest;
using System.Collections.Generic;
using System.Linq;

namespace Admin.NET.Application;
/// <summary>
/// 颜色服务
/// </summary>
[AllowAnonymous]
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class car_colorServiceApi : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<car_color> _rep;
    public car_colorServiceApi(SqlSugarRepository<car_color> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询颜色
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<car_tag>> Page(car_colorInput input)
    {
        var query = _rep.Context.Queryable<car_tag>()
        .Where(m => m.IsDelete == false && m.type == (int?)car_tagType.xilie)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.colorName), u => u.title.Contains(input.colorName.Trim()))
                     .WhereIF(input.pid >-1, u => u.pid == input.pid)
                  
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加颜色
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(Addcar_colorInput input)
    {
        var entity = input.Adapt<car_color>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除颜色
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(Deletecar_colorInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新颜色
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(Updatecar_colorInput input)
    {
        var entity = input.Adapt<car_color>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取颜色
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<car_color> Get([FromQuery] QueryByIdcar_colorInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取颜色列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<object> List([FromQuery] car_colorInput input)
    {

        var list = _rep.AsSugarClient().Queryable<car_color>()
            .WhereIF(input.carid != 0, m => m.carid == input.carid)
            //.WhereIF(!string.IsNullOrEmpty(input.colorName), it => it.colorName.Contains(input.colorName))
            .ToList();
        var xilielist = _rep.AsSugarClient().Queryable<car_xilie>().ToList();
        //商品组
        var group = list.GroupBy(m => m.xilieId).Select(g => new
        {
            groupname = xilielist.First(x => x.Id == g.Key).typename,
            list = g.ToList()
        });

        //搜索返回

   
        if (!string.IsNullOrEmpty(input.colorName))
        {
            var  searchlist = list.Where(it => it.colorName.Contains(input.colorName)).ToList();

            return new
            {
                group = group,
                search = searchlist,
            };
        }

        return new {
            group= group,
        };


    }





}

